/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sys/time.h>
#include <unistd.h>
#include "ability_env.h"

#include "popup_dialog.h"

namespace OHOS {

static int g_dialogResult = -1;

class DialogButtonClickListener : public UIView::OnClickListener {
public:
    DialogButtonClickListener(int &r) : result_(r) {}
    virtual ~DialogButtonClickListener(){}

    bool OnClick(UIView &view, const ClickEvent &event) override
    {
        g_dialogResult = result_;

        return true;
    }
private:
    int result_;
};

PopupDialog::PopupDialog()
{
    Task::Init();
    SetPeriod(TASK_TIME);
}

PopupDialog::~PopupDialog()
{
    if (backgroup_ != nullptr) {
        delete backgroup_;
        backgroup_ = nullptr;
    }

    if (background_ != nullptr) {
        delete background_;
        background_ = nullptr;
    }

    if (backicon_ != nullptr) {
        delete backicon_;
        backicon_ = nullptr;
    }

    if (okButton_ != nullptr) {
        delete okButton_;
        okButton_ = nullptr;
    }

    if (cancelButton_ != nullptr) {
        delete cancelButton_;
        cancelButton_ = nullptr;
    }

    if (msgText_ != nullptr) {
        delete msgText_;
        msgText_ = nullptr;
    }
}

void PopupDialog::Create(RootView *rootView, int x, int y, int w, int h)
{
    char iconPath[256] = {0};

    rootView_ = rootView;
    pathHead_ = GetSrcPath();

    backgroup_ = new UIViewGroup();
    backgroup_->SetPosition(x, y, w, h);
    backgroup_->SetStyle(STYLE_BACKGROUND_OPA, 0);

    (void)sprintf_s(iconPath, sizeof(iconPath), "%s/%s", pathHead_, POPWIN_BACK_GROUND);
    background_ = new UIImageView();
    background_->SetPosition(0, 0, w, h);
    background_->SetSrc((char *)iconPath);
    background_->SetStyle(STYLE_BACKGROUND_OPA, 0);
    backgroup_->Add(background_);

    backicon_ = new UIImageView();
    backicon_->SetPosition(0, 0, w, h);
    backicon_->SetSrc((char *)iconPath);
    backicon_->SetStyle(STYLE_BACKGROUND_OPA, 0);
    backgroup_->Add(backicon_);

    msgText_ = new UILabel();
    msgText_->SetPosition(RECV_SELECT_TEXT_X, RECV_SELECT_TEXT_Y, RECV_SELECT_TEXT_W, RECV_SELECT_TEXT_H);
    msgText_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
    msgText_->SetStyle(STYLE_BACKGROUND_OPA, 0);
    msgText_->SetFont(FONTNAME, FONT_SIZE);
    msgText_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::Black()));
    msgText_->SetText((char *)"弹窗提示");
    backgroup_->Add(msgText_);

    okButton_ = new UILabelButton();
    okButton_->SetPosition(LEFT_BUTTON_X, LEFT_BUTTON_Y, LEFT_BUTTON_W, LEFT_BUTTON_H);
    okButton_->SetText((char *)"接受");
    okButton_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray()));
    okButton_->SetStyle(STYLE_BORDER_RADIUS, BUTTON_RADIUS);
    okButton_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::Black()));
    okButton_->SetFont(FONTNAME, FONT_SIZE);
    okButton_->SetTouchable(true);
    DialogButtonClickListener *okClickListener = new DialogButtonClickListener(g_dialogResult);
    okButton_->SetOnClickListener(okClickListener);
    backgroup_->Add(okButton_);

    cancelButton_ = new UILabelButton();
    cancelButton_->SetPosition(RIGHT_BUTTON_X, RIGHT_BUTTON_Y, RIGHT_BUTTON_W, RIGHT_BUTTON_H);
    cancelButton_->SetText((char *)"取消");
    cancelButton_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray()));
    cancelButton_->SetStyle(STYLE_BORDER_RADIUS, BUTTON_RADIUS);
    cancelButton_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::Black()));
    cancelButton_->SetFont(FONTNAME, FONT_SIZE);
    cancelButton_->SetTouchable(true);
    DialogButtonClickListener *cancelClickListener = new DialogButtonClickListener(g_dialogResult);
    cancelButton_->SetOnClickListener(cancelClickListener);
    backgroup_->Add(cancelButton_);

    backgroup_->SetVisible(false);
    rootView_->Add(backgroup_);

    TaskExecute();
}

int PopupDialog::Display(const char *msg, int dialogType, int timeout)
{
    if (dialogType_ != dialogType) {
        timeout_ = timeout;
        g_dialogResult = -1;
        dialogType_ = dialogType;
    }

    message_ = msg;

    return 0;
}

int PopupDialog::GetResult()
{
    return g_dialogResult;
}

bool PopupDialog::GetDisplay()
{
    return display_;
}

void PopupDialog::Dismiss()
{
    backgroup_->SetVisible(false);
    backgroup_->Invalidate();
    display_ = false;
    timeout_ = 0;
    message_ = "";
    dialogType_ = DIALOG_TYPE_UNKNOW;
}

void PopupDialog::Callback()
{
    static int tmCnt = -1;
    static int lastType = DIALOG_TYPE_UNKNOW;

    if (lastType != dialogType_) {
        tmCnt = 0;
        lastType = dialogType_;
        display_ = false;
    }

    if (timeout_ > 0) {
        tmCnt++;
    }

    switch (dialogType_) {
        case DIALOG_TYPE_SELECT:
            ShowDialogSelect((const char *)message_.c_str());
            break;
        case DIALOG_TYPE_PROGRESS:
            ShowDialogProgress((const char *)message_.c_str());
            break;
        case DIALOG_TYPE_FINISH:
            ShowDialogFinish((const char *)message_.c_str());
            if (timeout_ > 0) {
                char tmp[128] = {0};
                (void)sprintf_s(tmp, sizeof(tmp), "返回(%d)", (timeout_ - tmCnt));
                cancelButton_->SetText((char *)tmp);
                cancelButton_->Invalidate();
            }
            break;
        default:
            return;
    }

    if (g_dialogResult != -1) {
        Dismiss();
        return;
    }

    if (timeout_ <= 0) {
        return;
    }

    if (tmCnt >= timeout_) {
        g_dialogResult = 0;
        Dismiss();
    }
}

void PopupDialog::ShowDialogSelect(const char *msg)
{
    if (display_ == false) {
        char iconPath[256] = {0};
        (void)sprintf_s(iconPath, sizeof(iconPath), "%s/%s", pathHead_, RECV_SELECT_ICON);
        backicon_->SetSrc((char *)iconPath);
        backicon_->Invalidate();

        cancelButton_->SetPosition(RIGHT_BUTTON_X, RIGHT_BUTTON_Y, RIGHT_BUTTON_W, RIGHT_BUTTON_H);
        cancelButton_->SetText((char *)"取消");
        cancelButton_->SetVisible(true);
        cancelButton_->Invalidate();

        okButton_->SetVisible(true);
        okButton_->Invalidate();

        if (msg != nullptr) {
            msgText_->SetText((char *)msg);
        }

        backgroup_->SetVisible(true);
        backgroup_->Invalidate();

        display_ = true;
    }
}

void PopupDialog::ShowDialogProgress(const char *msg)
{
    if (display_ == false) {
        char iconPath[256] = {0};
        (void)sprintf_s(iconPath, sizeof(iconPath), "%s/%s", pathHead_, RECV_PROGRESS_ICON);
        backicon_->SetSrc((char *)iconPath);
        backicon_->Invalidate();

        cancelButton_->SetVisible(false);
        cancelButton_->Invalidate();

        okButton_->SetVisible(false);
        okButton_->Invalidate();

        backgroup_->SetVisible(true);
        backgroup_->Invalidate();

        display_ = true;
    }

    if (msg != nullptr) {
        msgText_->SetText((char *)msg);
    }
}

void PopupDialog::ShowDialogFinish(const char *msg)
{
    if (display_ == false) {
        char iconPath[256] = {0};
        (void)sprintf_s(iconPath, sizeof(iconPath), "%s/%s", pathHead_, RECV_OK_ICON);
        backicon_->SetSrc((char *)iconPath);
        backicon_->Invalidate();

        cancelButton_->SetPosition(MIDDLE_BUTTON_X, MIDDLE_BUTTON_Y, MIDDLE_BUTTON_W, MIDDLE_BUTTON_H);
        cancelButton_->SetText((char *)"返回");
        cancelButton_->SetVisible(true);
        cancelButton_->Invalidate();

        okButton_->SetVisible(false);
        okButton_->Invalidate();

        if (msg != nullptr) {
            msgText_->SetText((char *)msg);
        }

        backgroup_->SetVisible(true);
        backgroup_->Invalidate();

        display_ = true;
    }
}

}
